package cn.woblog.recyclerviewsample.adapter;

import android.support.v4.util.SparseArrayCompat;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.Adapter;
import android.support.v7.widget.RecyclerView.LayoutManager;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;

/**
 * Created by Pich on 4/10/17.
 */

public class EnhanceAdapterWrapper extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

  private static final int TYPE_HEADER = 1000000;
  private static final int TYPE_FOOTER = 9000000;

  private final SparseArrayCompat<View> headerViews = new SparseArrayCompat<>();
  private final SparseArrayCompat<View> footerViews = new SparseArrayCompat<>();

  private final Adapter adapter;

  public EnhanceAdapterWrapper(RecyclerView.Adapter adapter) {
    this.adapter = adapter;
  }

  @Override
  public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    if (headerViews.get(viewType) != null) {
      return new ViewHolder(headerViews.get(viewType));
    } else if (footerViews.get(viewType) != null) {
      return new ViewHolder(footerViews.get(viewType));
    }
    return adapter.onCreateViewHolder(parent, viewType);
  }

  @Override
  public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    if (isHeaderViewPosition(position) || isFooterViewPosition(position)) {
      Log.d("TAG", "onBindViewHolder");
      return;
    }
    adapter.onBindViewHolder(holder, getItemRealPosition(position)); //必须要减去头部数量
  }

  public int getItemRealPosition(int position) {
    return position - getHeaderCount();
  }

  @Override
  public int getItemCount() {
    return getHeaderCount() + getItemRealCount() + getFooterCount();
  }

  @Override
  public int getItemViewType(int position) {
    if (isHeaderViewPosition(position)) {
      return headerViews.keyAt(position);
    } else if (isFooterViewPosition(position)) {
      return headerViews.keyAt(position - getHeaderCount() - getItemRealCount());
    }
    return adapter.getItemViewType(position - getHeaderCount());
  }


  public void addHeaderView(View view) {
    headerViews.put(TYPE_HEADER + headerViews.size(), changeLayoutParam(view));
  }


  public void addFooterView(View view) {
    footerViews.put(TYPE_FOOTER + footerViews.size(), changeLayoutParam(view));
  }

  private View changeLayoutParam(View view) {
    view.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
    return view;
  }

  public boolean isHeaderViewPosition(int position) {
    return position < getHeaderCount();
  }

  public boolean isFooterViewPosition(int position) {
    return position >= getHeaderCount() + getItemRealCount();
  }

  public int getItemRealCount() {
    return adapter.getItemCount();
  }

  public int getHeaderCount() {
    return headerViews.size();
  }

  public int getFooterCount() {
    return footerViews.size();
  }

  @Override
  public void onAttachedToRecyclerView(RecyclerView recyclerView) {
    adapter.onAttachedToRecyclerView(recyclerView);

    final LayoutManager layoutManager = recyclerView.getLayoutManager();
    if (layoutManager instanceof GridLayoutManager) {
      ((GridLayoutManager) layoutManager).setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
        @Override
        public int getSpanSize(int position) {
          int viewType = getItemViewType(position);
          if (headerViews.get(viewType) != null) {
            return ((GridLayoutManager) layoutManager).getSpanCount(); //占spancount这么多行
          } else if (footerViews.get(viewType) != null) {
            return ((GridLayoutManager) layoutManager).getSpanCount();
          }
          return 1; //占用一格
        }
      });
    }
  }

  @Override
  public void onViewAttachedToWindow(RecyclerView.ViewHolder holder) {
    adapter.onViewAttachedToWindow(holder);

    int layoutPosition = holder.getLayoutPosition();
    if (isHeaderViewPosition(layoutPosition) || isFooterViewPosition(layoutPosition)) {
      //如果是头部，底部
      LayoutParams layoutParams = holder.itemView.getLayoutParams();
      if (layoutParams != null && layoutParams instanceof StaggeredGridLayoutManager.LayoutParams) {
        ((StaggeredGridLayoutManager.LayoutParams) layoutParams).setFullSpan(true);
      }
    }
  }

  public class ViewHolder extends RecyclerView.ViewHolder {

    public ViewHolder(View itemView) {
      super(itemView);
    }
  }
}
